package com.gxa.jjty.admin.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gxa.jjty.admin.mapper.EvaluateImgMapper;
import com.gxa.jjty.admin.mapper.EvaluateMapper;
import com.gxa.jjty.admin.mapper.EvaluateVOMapper;
import com.gxa.jjty.admin.service.EvaluateService;
import com.gxa.jjty.core.pojo.Evaluate;
import com.gxa.jjty.core.pojo.EvaluateImg;
import com.gxa.jjty.core.pojo.EvaluateVO;
import com.gxa.jjty.core.service.impl.BaseServiceImpl;
import com.gxa.jjty.core.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.io.Serializable;
import java.lang.reflect.Field;
import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName EvaluateServiceImpl
 * @Author: ws
 * @date 2021/12/8 10:02
 */
@Service("evaluateService")
public class EvaluateServiceImpl extends BaseServiceImpl<Evaluate> implements EvaluateService {

    @Autowired
    EvaluateMapper evaluateMapper;

    @Autowired
    EvaluateVOMapper evaluateVOMapper;

    @Autowired
    EvaluateImgMapper evaluateImgMapper;

    @Override
    public void deleteByOrderId(Long orderId) {

        Evaluate evaluate = new Evaluate();
        evaluate.setOrderId(orderId);
        evaluateMapper.delete(evaluate);

        EvaluateImg evaluateImg = new EvaluateImg();
        evaluateImg.setOrderId(orderId);
        evaluateImgMapper.delete(evaluateImg);
    }

    @Override
    public Map selectByOrderId(Long orderId) {

        EvaluateVO evaluateVO = new EvaluateVO();
        evaluateVO.setOrderId(orderId);
        EvaluateVO evaluateVO1 = evaluateVOMapper.selectOne(evaluateVO);

        EvaluateImg evaluateImg = new EvaluateImg();
        evaluateImg.setOrderId(orderId);
        List<EvaluateImg> select = evaluateImgMapper.select(evaluateImg);

        Map<String,Object> result = new HashMap<>(2);
        result.put("evaluate",evaluateVO1);
        result.put("imgUrls",select);

        return result;
    }

    @Override
    public PageInfo<EvaluateVO> selectByPage(Integer page, Integer size) {
        PageHelper.startPage(page, size);
        Example example = new Example(EvaluateVO.class);

        //实现逆序
        example.orderBy("evaluateTime").desc();
        List<EvaluateVO> ts = evaluateVOMapper.selectByExample(example);


        PageInfo<EvaluateVO> list = PageInfo.of(ts);
        return list;
    }

    @Override
    public PageInfo<EvaluateVO> selectByTimeAnd(Integer page, Integer size, Timestamp from, Timestamp to,
                                                String name, String phyName,String orderNumber){
        PageHelper.startPage(page,size);

        Example example = new Example(EvaluateVO.class);
        Example.Criteria criteria = example.createCriteria();
        example.orderBy("evaluateTime").desc();

        if (!StringUtil.isEmpty(name)) {
            criteria.andLike("name","%"+name + "%");
        }
        if (!StringUtil.isEmpty(phyName)) {
            criteria.andLike("phyName","%"+phyName + "%");
        }
        if (!StringUtil.isEmpty(orderNumber)) {
            criteria.andLike("orderNumber","%"+orderNumber + "%");
        }
        if (from != null && to != null) {
            criteria.andBetween("evaluateTime",from,to);
        }

        List<EvaluateVO> list = evaluateVOMapper.selectByExample(example);

        return PageInfo.of(list);
    }
}
